package com.example.sunmiv1.adapter;

import android.opengl.Visibility;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.example.sunmiv1.R;
import com.example.sunmiv1.entity.UserRepaymentInfo;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class RepayAdapter extends BaseAdapter {

    private List<UserRepaymentInfo.DataBean.ListBean> list;
    private ViewHolder vh;
    private boolean flag = false;
    private CallBack callBack;

    public RepayAdapter(List<UserRepaymentInfo.DataBean.ListBean> list) {
        this.list = list;
    }

    public void setCheckFlag(boolean flag) {
        this.flag = flag;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return list.size();
    }

    @Override
    public Object getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
//        if (convertView == null) {
        convertView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_repay, null);
        vh = new ViewHolder(convertView);
//        }
        UserRepaymentInfo.DataBean.ListBean bean = list.get(position);
        if (bean.isCheck()) {
            vh.itemRepayCheckImg.setImageResource(R.mipmap.repay_x);
        } else {
            vh.itemRepayCheckImg.setImageResource(R.mipmap.repay_wx);
        }
        vh.itemRepayOrderno.setText(bean.getOrder_no());
        vh.itemRepayTime.setText(bean.getAdd_time());
        vh.itemRepayCreditMoney.setText(bean.getBill_money());
        vh.itemRepayRepayMoney.setText(bean.getMoney());
        if (!TextUtils.isEmpty(bean.getCustomMoney())) {
            bean.setRepaymentMoney(bean.getCustomMoney());
        } else {
            bean.setRepaymentMoney(bean.getMoney());
        }
//        vh.itemRepayMoneyTv.setText(bean.getRepaymentMoney());
        vh.itemRepayMoneyEdit.setText(bean.getRepaymentMoney());
        if (flag) {
            vh.itemRepayEditMoney.setClickable(false);
            vh.itemRepayRepayMoney.setEnabled(false);
        }
        final ImageView img = vh.itemRepayCheckImg;
        vh.itemRepayCheckLin.setOnClickListener(v -> {
            if (flag) {
                return;
            }
            if (bean.isCheck()) {
                img.setImageResource(R.mipmap.repay_wx);
            } else {
                img.setImageResource(R.mipmap.repay_x);
            }
            bean.setCheck(!bean.isCheck());
            callBack.click();
        });

        final TextView editMoney = vh.itemRepayEditMoney;
//        final TextView moneyTv = vh.itemRepayMoneyTv;
        final EditText editText = vh.itemRepayMoneyEdit;
//        editMoney.setOnClickListener(v -> {
//            if (editText.getVisibility() == View.VISIBLE) {
//                moneyTv.setVisibility(View.VISIBLE);
//                editText.setVisibility(View.GONE);
//                moneyTv.setText(bean.getRepaymentMoney());
//                callBack.click();
//            } else {
//                moneyTv.setVisibility(View.GONE);
//                editText.setVisibility(View.VISIBLE);
//                editText.postDelayed(() -> editText.requestFocus(), 500);
//            }
//        });
        final int index = position;
        editText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
                if (charSequence.toString().contains(".")) {
                    if (-1 != charSequence.toString().indexOf(".", charSequence.toString().indexOf(".") + 1)) {
                        editText.setText(charSequence.toString().
                                substring(0, charSequence.toString().length() - 1));
                        editText.setSelection(charSequence.length() - 1);
                    } else {
                        if (charSequence.length() - 1 - charSequence.toString().indexOf(".") > 2) {
                            charSequence = charSequence.toString().subSequence(0, charSequence.toString().indexOf(".") + 3);
                            editText.setText(charSequence);
                            editText.setSelection(charSequence.length());
                        }
                    }
                }
                if (".".equals(charSequence.toString().trim().substring(0))) {
                    charSequence = "0" + charSequence;
                    editText.setText(charSequence);
                    editText.setSelection(2);
                }

                if (charSequence.toString().startsWith("0") && charSequence.toString().trim().length() > 1) {
                    if (!".".equals(charSequence.toString().substring(1, 2))) {
                        editText.setText(charSequence.subSequence(1, 2));
                        editText.setSelection(1);
                        return;
                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {
                list.get(index).setRepaymentMoney(editText.getText().toString());
                if (!TextUtils.isEmpty(editText.getText().toString().trim())) {
                    callBack.click();
                }
            }
        });
        return convertView;
    }

    public interface CallBack {
        void click();//type:1增加 2:減少
    }

    public void setCallBack(CallBack callBack) {
        this.callBack = callBack;
    }

    static class ViewHolder {
        @BindView(R.id.itemRepay_checkImg)
        ImageView itemRepayCheckImg;
        @BindView(R.id.itemRepay_checkLin)
        LinearLayout itemRepayCheckLin;
        @BindView(R.id.itemRepay_orderno)
        TextView itemRepayOrderno;
        @BindView(R.id.itemRepay_creditMoney)
        TextView itemRepayCreditMoney;
        @BindView(R.id.itemRepay_time)
        TextView itemRepayTime;
        @BindView(R.id.itemRepay_repayMoney)
        TextView itemRepayRepayMoney;
//        @BindView(R.id.itemRepay_moneyTv)
//        TextView itemRepayMoneyTv;
        @BindView(R.id.itemRepay_moneyEdit)
        EditText itemRepayMoneyEdit;
        @BindView(R.id.itemRepay_editMoney)
        TextView itemRepayEditMoney;

        ViewHolder(View view) {
            ButterKnife.bind(this, view);
        }
    }
}
